import React, { useEffect, useState } from 'react';
import { Tag, Button, message } from 'antd';
import { useTranslation } from 'react-i18next';
import { APPROVAL_STATUS, RES_MODE } from '../../../utils/constant';
import { getUserName, getPageOperationPermission, hasCampAdminRole, hasBuRole, getPagePermissionByUrl } from '../../../utils/commonUtil';
import DeleteConfirm from '../DeleteConfirm';
import { apiSynApprovalCenterApproval } from '../../../services/approvalCenter';
import { useNavigate } from 'react-router-dom';
import { PAGE_PERMISSION_CHECK } from '../../../utils/pagePermission';

const RequestApprovalBtn = ({
  editMode,
  initialValues,
  form,
  approveRequestSituationId,
  moduleConfig,
}) => {
  const { t } = useTranslation();
  const { APPROVED, REJECTED, PENDING, DRAFT } = APPROVAL_STATUS;
  const navigate = useNavigate();
  const [hasApproval, setHasApproval] = useState(false);
  const [hasImportTestData, setHasImportTestData] = useState(false);

  // mock permission
  const isCampAdmin = true;
  const isBuPRPD = false;
  const isBuAdmin = false;
  const isBuTester = true;

  // 审批操作
  const approveClickHandle = (type) => {
    if (type === APPROVED) {
      form.validateFields().then(() => {
        DeleteConfirm({
          t,
          title: t('common.approveConfrimTitle', { type }),
          content: t('common.approveConfrimDes', { type }),
          onOk () {
            apiSynApprovalCenterApproval({
              approveRequestSituationId: approveRequestSituationId,
              assign: form.getFieldsValue()?.project?.assign?.fieldValue,
              requestStatus: type,
              userId: getUserName(),
            }).then((res) => {
              if (res?.data?.code === 200) {
                message.success(type === APPROVED ? t('common.approveMsg') : t('common.rejectMsg'));
                moduleConfig?.listUrl && navigate(moduleConfig?.listUrl);
              }
            });
          }
        });
      }, () => {
        return false;
      });
    } else {
      DeleteConfirm({
        t,
        title: t('common.approveConfrimTitle', { type }),
        content: t('common.approveConfrimDes', { type }),
        onOk () {
          apiSynApprovalCenterApproval({
            approveRequestSituationId: approveRequestSituationId,
            assign: '',
            requestStatus: type,
            userId: getUserName(),
          }).then((res) => {
            if (res?.data?.code === 200) {
              message.success(type === APPROVED ? t('common.approveMsg') : t('common.rejectMsg'));
              moduleConfig?.listUrl && navigate(moduleConfig?.listUrl);
            }
          });
        }
      });
    }

  };
  const closeClickHandle = () => {
    moduleConfig?.listUrl && navigate(moduleConfig?.listUrl);
  };

  // 操作鉴权
  const operateAuthentication = () => {
    // 审批权限
    const permissionList = getPagePermissionByUrl(PAGE_PERMISSION_CHECK, 'approvalCenter');
    const approvalPermission = getPageOperationPermission(permissionList, 'approval');
    const hasApproval = hasCampAdminRole(approvalPermission) || hasBuRole(approvalPermission);
    setHasApproval(hasApproval);
  };

  useEffect(() => {
    operateAuthentication();
  }, []);


  return (
    <>
      {
        form &&
        <>
          {
            (hasApproval) && editMode === RES_MODE.APPROVAL && initialValues?.project?.requestStatus?.fieldValue === PENDING ?
              <>
                <Button className='zr-reject-btn' onClick={() => { approveClickHandle(REJECTED) }} style={{ marginRight: '10px' }}>{t('common.reject')}</Button>
                <Button type='primary' className='zr-approve-btn' onClick={() => { approveClickHandle(APPROVED) }}>{t('common.approve')}</Button>
              </>
              :
              editMode === RES_MODE.VIEW &&
              <Button Button onClick={() => { closeClickHandle(APPROVED) }}>{t('common.close')}</Button>
          }
        </>
      }
    </>
  )
};
export default RequestApprovalBtn;